﻿using M_Purchase.Api.Appliction.Command.PurchCommand;
using M_Purchase.Domain;
using M_Purchase.Domain.DTO;
using M_Purchase.Repository.PurchaseInterface;
using MediatR;

namespace M_Purchase.Api.Appliction.CommandHandlers.PurchCommandHandler
{
    /// <summary>
    /// 根据采购ID显示供应商信息
    /// </summary>
    public class SupplierListPurDCommandHandler : IRequestHandler<SupplierListPurDCommand, List<SupDto>>
    {
        /// <summary>
        /// 供应商
        /// </summary>
        private readonly ISupplierRepository _supplierRepository;
        /// <summary>
        /// 采购消息
        /// </summary>
      private readonly IPurchRepository _purchRepository;
        public SupplierListPurDCommandHandler(ISupplierRepository supplierRepository, IPurchRepository purchRepository)
        {
            _supplierRepository = supplierRepository;
            _purchRepository = purchRepository;
        }
        public async Task<List<SupDto>> Handle(SupplierListPurDCommand request, CancellationToken cancellationToken)
        {
           var SupList=from a in await _supplierRepository.GetAllAsync()
                       join b in await _purchRepository.GetAllAsync()
                       on a.SupplierID equals b.SupplierID
                       where b.PurchaseID == request.PurchaseID
                       select new SupDto()
                       {
                           SupplierID = a.SupplierID,
                           SupplierName = a.SupplierName,
                           SupplierCode=a.SupplierCode,
                           Curator=a.Curator,
                           CuratorPhone=a.CuratorPhone,
                           PurchaseID=b.PurchaseID
                       };
            return SupList.ToList();

        }
    }
}
